using System;
using Oracle.ManagedDataAccess.Client;
using OracleInternal.Common;

namespace OracleInternal.TTC
{
	internal class TTCBlob : TTCLob
	{
		internal TTCBlob(MarshallingEngine mEngine)
			: base(mEngine)
		{
		}

		internal override byte[] CreateTemporaryLob(bool bCache, bool bNClob, int duration)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCBlob, OracleTraceFuncName.CreateTemporaryLob);
			}
			try
			{
				if (12 == duration)
				{
					throw new Exception("Invalid duration in CreateTemporaryLob");
				}
				Initialize();
				m_lobOperation = 272L;
				m_sourceLobLocator = new byte[40];
				m_sourceLobLocator[1] = 84;
				m_characterSet = 1;
				m_lobAmount = duration;
				m_bSendLobAmount = true;
				m_destinationOffset = 113L;
				m_destinationLength = duration;
				m_bNullO2U = true;
				m_lobSCN = new int[1];
				m_lobSCN[0] = (bCache ? 1 : 0);
				m_lobSCNLength = 1;
				WriteFunctionHeader();
				WriteLobOperation();
				m_marshallingEngine.m_oraBufWriter.FlushData();
				ReceiveResponse();
				return m_sourceLobLocator;
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCBlob, OracleTraceFuncName.CreateTemporaryLob, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCBlob, OracleTraceFuncName.CreateTemporaryLob);
				}
			}
		}

		internal bool Open(byte[] lobLocator, int mode)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCBlob, OracleTraceFuncName.Open);
			}
			try
			{
				int mode2 = 2;
				if (mode == 0)
				{
					mode2 = 1;
				}
				return OpenLob(lobLocator, mode2, 32768);
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCBlob, OracleTraceFuncName.Open, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCBlob, OracleTraceFuncName.Open);
				}
			}
		}

		internal bool Close(byte[] lobLocator)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCBlob, OracleTraceFuncName.Close);
			}
			try
			{
				return CloseLob(lobLocator, 65536);
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCBlob, OracleTraceFuncName.Close, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCBlob, OracleTraceFuncName.Close);
				}
			}
		}
	}
}
